$(document).ready(function() {
  require(["inputdrop", "tree"], function(inputdrop, tree) {
    var list_col = [
      { text: "", name: "t", width: 3, hideSearch: "hide" },
      { text: "类型名称", name: "name", align: "left", width: 40,tip:true },
      {
        text: "所属资产类别",
        name: "categoryName",
        align: "left",
        tip:true,
        width: 40
      }
    ];
    var list_oper = [
      { icon: "rh-icon rh-edit", text: "编辑", aclick: open_asset_type_dialog },

      { icon: "rh-icon rh-delete", text: "删除", aclick: type_del }
    ];

    var el_table_div = $("#table_div1");

    var current_selected_node_id = "";

    // var current_selected_node_name = "事件类型";

    var key;
    event_init();
    $("input[placeholder]").placeholder();
    event_class_tree();
    asset_type_list({
      paramObj: { pageSize: 50 },
      isLoad: true,
      maskObj: "body"
    });
    function view_init() {
      $("#content_div").css("padding", "0");
      $("#pg-content").css("padding", "0");
    }

    function event_init() {
      $("#add_type_btn").click(function() {
        open_asset_type_dialog();
      });
      $("#add_btn").click(function() {
        open_asset_class_dialog();
      });

      $("#searchIcon").click(function() {
        key = $("#searchKey").val();
        asset_type_list({
          paramObj: {
            keyword: key,
            categoryId: current_selected_node_id
          },
          isLoad: true,
          maskObj: "body"
        });
      });
      $("#searchKey").keydown(function(event) {
        if (event.keyCode == 13) {
          $("#searchIcon").trigger("click");
        }
      });
    }

    function event_class_tree(opt) {
      if (opt) {
        current_selected_node_id = opt;
      }
      xd_ajax_get({
        url: "api/property/type/countByCategory",
        isLoad: false,
        successCallBack: function(data) {
          tree.render($("#event_class_tree"), {
            pId: "parentID",
            label: "label",
            id: "id",
            zNodes: data,
            expandNode: -1,
            selectNode: current_selected_node_id,
            zTreeOnClick: function(event, treeId, treeNode) {
              current_selected_node_id = treeNode.id;
              // current_selected_node_name = treeNode.label;
              var paramObj = { categoryId: treeNode.id };
              if (treeNode.id == "-1") {
                paramObj = null;
              }
              asset_type_list({
                paramObj: paramObj,
                isLoad: true,
                maskObj: "body"
              });
            }
          });
        }
      });
    }

    function asset_type_list(option) {
      g_grid.render($("#table_div1"), {
        header: list_col,
        url: "api/property/type/list",
        type: "xd",
        dataKey: "list",
        oper: list_oper,
        operWidth: "60px",
        paramObj: option.paramObj,
        queryBefore: function(paramObj) {
          if (current_selected_node_id != -1) {
            paramObj.categoryId = current_selected_node_id;
          }
        },
        isLoad: option.isLoad,
        maskObj: option.maskObj,
        hasBorder: false,
        hideSearch: true,
        autoHeight: true,
        showCount: true,
        allowCheckBox: false
      });
    }

    function open_asset_type_dialog(rowData) {
      // 弹出修改框
      $.ajax({
        type: "GET",
        url: "module/base_resource_mant/asset_type_tpl.html",
        success: function(data) {
          g_dialog.dialog($(data).find("[id=asset_type_tpl]"), {
            width: "550px",
            init: init,
            initAfter: initAfter,
            title: "资产类型",
            saveclick: save_click
          });
        }
      });

      function init(el) {
        // g_validate.init(el);
        xd_ajax_get({
          url: "api/property/category/list",
          paramObj: "",
          successCallBack: function(data) {
            var dataList = [];
            data.forEach(function(item) {
              dataList.push({ id: item.code, text: item.name });
            }, this);

            $("#assetType").select2({
              data: dataList,
              placeholder: "请选择"
            });

            if (rowData) {
              el.umDataBind("render", rowData);
            }
          }
        });
      }
      function initAfter(el, saveObj) {
        $("#assetTypeName").blur(function() {
          var param = {};
          param.name = $("#assetTypeName").val();
          param.id = $("#assetTypeId").val();
          if(!param.name){
            return false
          }
          xd_ajax_get({
            url: "api/property/type/checkName",
            paramObj: param,
            successCallBack: function(data) {
              if (!data) {
                change_error_style($("#assetTypeName"), "add");
                change_tip($("#assetTypeName"), null, null, "add", "资产类型名称已存在");
              }
            }
          });
        });
      }

      function save_click(el, saveObj) {
        // 校验等级定义是否已填
        if (validateForm($("#update_form"))) {
          xd_ajax_post({
            url: "api/property/type/save",
            paramObj: saveObj,
            maskObj: el,
            successCallBack: function(data) {
              // 关闭对话框
              g_dialog.hide(el);
              // 弹出成功提示
              g_dialog.operateAlert();
              // 重新刷新列表
              asset_type_list({
                paramObj: {
                  keyword: key,
                  categoryId: current_selected_node_id
                },
                isLoad: true,
                maskObj: "body"
              });
              event_class_tree(current_selected_node_id);
            }
          });
        }
      }
    }
    function open_asset_class_dialog() {
      // 弹出修改框
      $.ajax({
        type: "GET",
        url: "module/base_resource_mant/asset_type_tpl.html",
        success: function(data) {
          g_dialog.dialog($(data).find("[id=asset_class_tpl]"), {
            width: "550px",
            init: init,
            initAfter: initAfter,
            title: "资产类别",
            saveclick: save_click
          });
        }
      });
      function init(el) {
        // g_validate.init(el);
      }
      function initAfter(el, saveObj) {
        $("#assetClassName").blur(function() {
          var param = {};
          param.name = $("#assetClassName").val();
          if(!param.name){
            return false
          }
          xd_ajax_get({
            url: "api/property/category/checkName",
            paramObj: param,
            successCallBack: function(data) {
              if (!data) {
                change_error_style($("#assetClassName"), "add");
                change_tip(
                  $("#assetClassName"),
                  null,
                  null,
                  "add",
                  "资产类别名称已存在"
                );
              }
            }
          });
        });
      }
      function save_click(el, saveObj) {
        // 校验等级定义是否已填
        if (validateForm($("#add_asset_class"))) {
          xd_ajax_post({
            url: "api/property/category/add",
            paramObj: saveObj,
            maskObj: el,
            successCallBack: function() {
              // 关闭对话框
              g_dialog.hide(el);
              // 弹出成功提示
              g_dialog.operateAlert();
              // 重新刷新列表
              asset_type_list({
                paramObj: saveObj,
                isLoad: false
              });

              event_class_tree(current_selected_node_id);
            }
          });
        }
      }
    }

    /**
     * 
     * @desc {{description}}{{删除表格单元格数据}}
     * @param {any} rowData 单元格数据对象
     */
    function type_del(rowData) {
      var ids = [];
      ids.push(rowData.id);
      xd_ajax_post({
        url: "api/property/type/checkRelated",
        paramObj: ids,
        successCallBack: function(data) {
          if (data) {
            g_dialog.operateAlert(null, "当前资产类型存在关联资产，无法删除", "error");
          } else {
            g_dialog.operateConfirm("确认删除此类型？", {
              saveclick: function() {
                xd_ajax_post({
                  url: "api/property/type/delete",
                  paramObj: ids,
                  successCallBack: function(data) {
                    asset_type_list({ maskObj: $("#table_div") });
                    g_dialog.operateAlert(null, "操作成功！");
                    asset_type_list({
                      paramObj: {
                        keyword: key,
                        categoryId: current_selected_node_id
                      },
                      isLoad: true,
                      maskObj: "body"
                    });
                  }
                });
              }
            });
          }
        }
      });
    }
  });
});
